use serde::Deserialize;

#[derive(Deserialize, Debug)]
pub struct WebCfg {
    pub host: String,
    pub port: String,
}

#[derive(Deserialize, Debug)]
pub struct Configuration {
    pub web: WebCfg,
}

impl Configuration {
    pub fn get_bind_listener(&self) -> String {
        return format!("{}:{}", self.web.host, self.web.port);
    }
    pub  async fn from_env() -> Result<Self, anyhow::Error> {
        let config_result = config::Config::builder()
            .add_source(config::Environment::default())
            .build();

        match config_result {
            Ok(config) => {
                let cfg: Result<Configuration, config::ConfigError> = config.try_deserialize();
                Ok(cfg?)
            }
            Err(err) => Err(anyhow::Error::msg(err.to_string())),
        }
    }
}
